package diploma.kisel.modelas.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.ComboBoxModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;

import diploma.kisel.modelas.domain.ComputerType;
import diploma.kisel.modelas.domain.Device;
import diploma.kisel.modelas.domain.DeviceType;
import diploma.kisel.modelas.service.DeviceServiceImpl;
import diploma.kisel.modelas.service.DeviceTypeServiceImpl;
import diploma.kisel.modelas.service.api.DeviceService;
import diploma.kisel.modelas.service.api.DeviceTypeService;
import diploma.kisel.modelas.validation.Validation;

/**
 * This window is used to create, edit and delete devices.
 * 
 * @author Nikolay Kisel
 *
 */
public class DeviceWindow extends JFrame {

	private JPanel contentPane;
	private JTextField textFieldNumber;
	private JTextField textFieldBrand;
	private JTextField textFieldCost;
	private JTextArea textArea;
	private JButton btnDelete;
	private JComboBox comboBox;
	
	private Device device;
	
	private DeviceService deviceService = new DeviceServiceImpl();
	private DeviceTypeService deviceTypeService = new DeviceTypeServiceImpl();
	
	private static DeviceWindow frame;
	
	private final int deviceNumberLength = 10;
	private final int deviceBrandLength = 45;
	private final int deviceFeaturesLength = 250;

	
	/**
	 * Create the frame.
	 */
	public DeviceWindow() {
		setBounds(100, 100, 492, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		SpringLayout sl_contentPane = new SpringLayout();
		contentPane.setLayout(sl_contentPane);
		
		JLabel label = new JLabel("Номер:");
		contentPane.add(label);
		
		textFieldNumber = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, label, 3, SpringLayout.NORTH, textFieldNumber);
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldNumber, 10, SpringLayout.NORTH, contentPane);
		contentPane.add(textFieldNumber);
		textFieldNumber.setColumns(10);
		
		JLabel label_1 = new JLabel("Марка:");
		sl_contentPane.putConstraint(SpringLayout.WEST, label_1, 10, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, label, 0, SpringLayout.EAST, label_1);
		contentPane.add(label_1);
		
		textFieldBrand = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.WEST, textFieldNumber, 0, SpringLayout.WEST, textFieldBrand);
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldBrand, -3, SpringLayout.NORTH, label_1);
		contentPane.add(textFieldBrand);
		textFieldBrand.setColumns(10);
		
		JLabel label_2 = new JLabel("Стоимость:");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label_2, 132, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, label_1, -44, SpringLayout.NORTH, label_2);
		sl_contentPane.putConstraint(SpringLayout.WEST, label_2, 10, SpringLayout.WEST, contentPane);
		contentPane.add(label_2);
		
		textFieldCost = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.WEST, textFieldBrand, 0, SpringLayout.WEST, textFieldCost);
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldCost, -3, SpringLayout.NORTH, label_2);
		sl_contentPane.putConstraint(SpringLayout.WEST, textFieldCost, 25, SpringLayout.EAST, label_2);
		contentPane.add(textFieldCost);
		textFieldCost.setColumns(10);
		
//		ButtonGroup butGroup = new ButtonGroup();
		
		JLabel label_3 = new JLabel("Характеристика:");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label_3, 0, SpringLayout.NORTH, label);
		contentPane.add(label_3);
		
		JScrollPane scrollPane = new JScrollPane();
		sl_contentPane.putConstraint(SpringLayout.NORTH, scrollPane, 16, SpringLayout.SOUTH, label_3);
		sl_contentPane.putConstraint(SpringLayout.WEST, scrollPane, 62, SpringLayout.EAST, textFieldBrand);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, scrollPane, -139, SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, scrollPane, -17, SpringLayout.EAST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, label_3, 0, SpringLayout.WEST, scrollPane);
		contentPane.add(scrollPane);
		
		textArea = new JTextArea();
		//contentPane.add(textArea);
		scrollPane.setViewportView(textArea);
		
		JButton btnSave = new JButton("Save");
		sl_contentPane.putConstraint(SpringLayout.SOUTH, btnSave, -10, SpringLayout.SOUTH, contentPane);
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				saveDevice();
			}
		});
		contentPane.add(btnSave);
		
		btnDelete = new JButton("Delete");
		sl_contentPane.putConstraint(SpringLayout.NORTH, btnDelete, 0, SpringLayout.NORTH, btnSave);
		sl_contentPane.putConstraint(SpringLayout.EAST, btnDelete, -6, SpringLayout.WEST, btnSave);
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				deleteDevice();
			}
		});
		contentPane.add(btnDelete);
		
		JButton btnCancel = new JButton("Cancel");
		sl_contentPane.putConstraint(SpringLayout.EAST, btnSave, -6, SpringLayout.WEST, btnCancel);
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setVisible(false);
			}
		});
		sl_contentPane.putConstraint(SpringLayout.SOUTH, btnCancel, -10, SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, btnCancel, 0, SpringLayout.EAST, scrollPane);
		contentPane.add(btnCancel);
		
		comboBox = createComboBox();
		sl_contentPane.putConstraint(SpringLayout.NORTH, comboBox, 34, SpringLayout.SOUTH, textFieldCost);
		sl_contentPane.putConstraint(SpringLayout.WEST, comboBox, 0, SpringLayout.WEST, textFieldNumber);
		contentPane.add(comboBox);
		
		JLabel label_4 = new JLabel("Тип");
		sl_contentPane.putConstraint(SpringLayout.WEST, label_4, 0, SpringLayout.WEST, label);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, label_4, 0, SpringLayout.SOUTH, comboBox);
		contentPane.add(label_4);
	}

	public static DeviceWindow getFrame() {
		return frame;
	}

	public static void setFrame(DeviceWindow frame) {
		DeviceWindow.frame = frame;
	}

	public Device getDevice() {
		return device;
	}

	public void setDevice(Device device) {
		this.device = device;
	}
	
	public void init() {
		if (device!=null) {
			textFieldNumber.setText(device.getNumber());
			textFieldBrand.setText(device.getBrand());
			textFieldCost.setText(Integer.toString(device.getCost()));
			comboBox.setSelectedItem(device.getDeviceType().getId());
			textArea.setText(device.getFeatures());
		}
	}
	
	public JComboBox createComboBox() {
		List<DeviceType> deviceTypes = deviceTypeService.findAll();
		Vector<Object> items = new Vector<Object>();
		for (DeviceType deviceType : deviceTypes) {
			items.add(deviceType.getId());
		}
		comboBox = new JComboBox(items);
//		comboBox.setSelectedItem(device.getDeviceType().getId());
		return comboBox;
	}
	
	public void saveDevice() {
		String fieldNumber = textFieldNumber.getText();
		if (Validation.isLengthBetween(fieldNumber, 1, deviceNumberLength))
			device.setNumber(fieldNumber);
		else {
			JOptionPane.showMessageDialog(frame,
					"Неверное название устройства. Название должно быть до 10 символов.");
			return;
		}
		String fieldBrand = textFieldBrand.getText();
		if (Validation.isLengthBetween(fieldBrand, 1, deviceBrandLength))
			device.setBrand(fieldBrand);
		else {
			JOptionPane.showMessageDialog(frame,
					"Неверное название марки. Название должно быть до 45 символов.");
			return;
		}
		String fieldFeatures = textArea.getText();
		if (Validation.isLengthBetween(fieldFeatures, 0, deviceFeaturesLength))
			device.setFeatures(fieldFeatures);
		else {
			JOptionPane.showMessageDialog(frame,
					"Неверное описание характеристик. Характеристика может включать до 250 символов");
			return;
		}
		device.setFeatures(fieldFeatures);
		String fieldCost = textFieldCost.getText();
		if (Validation.isNumber(fieldCost))
			device.setCost(Integer.valueOf(fieldCost));
		else {
			JOptionPane.showMessageDialog(frame,
					"Указана неверная цена");
			return;
		}
		DeviceType deviceType = deviceTypeService.findById((String)comboBox.getSelectedItem());
		device.setDeviceType(deviceType);
		deviceService.save(device);
		JOptionPane.showMessageDialog(frame,
				"Устройство сохранено");
		DeviceCatalogWindow.setNewModel(deviceService.findAll());
		
	}
	
	public void deleteDevice() {
		int response = JOptionPane.showConfirmDialog(frame, "Удалить данное устройство?");
		if (response == JOptionPane.YES_OPTION) {
			deviceService.remove(device.getId());
			DeviceCatalogWindow.setNewModel(deviceService.findAll());
			frame.setVisible(false);
		}
			
	}
	
	public JButton getBtnDelete() {
		return btnDelete;
	}

	public void setBtnDelete(JButton btnDelete) {
		this.btnDelete = btnDelete;
	}	
}
